//BeanCipher - Bean Cipher Security Layer
//Copyright (C) 2005 Raul Alfredo Bajales
//
//This library is free software; you can redistribute it and/or
//modify it under the terms of the GNU Lesser General Public
//License as published by the Free Software Foundation; either
//version 2.1 of the License, or (at your option) any later version.
//
//This library is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
//Lesser General Public License for more details.
//
//You should have received a copy of the GNU Lesser General Public
//License along with this library; if not, write to the Free Software
//Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
package ar.com.rab.beancipher.impl;

import java.lang.reflect.InvocationTargetException;
import java.util.Map;

import ar.com.rab.beancipher.spi.IConstants;
import ar.com.rab.beancipher.spi.IPropertyCipher;
import ar.com.rab.beancipher.spi.IPropertyDescriptor;
import ar.com.rab.beancipher.spi.ITypeCipherAdapter;
import ar.com.rab.beancipher.spi.exceptions.PropertyCipherException;
import ar.com.rab.beancipher.spi.exceptions.TypeCipherException;

/**
 * Contains a map of specific type ciphers (class name / type cipher reference).
 * Can apply a corresponding type cipher to a property of a bean for cipher / decipher.
 * 
 * @author Bajales Raul
 */
public class PropertyCipher implements IPropertyCipher {

    private ITypeCipherAdapter defaultTypeCipher;
    private Map specificTypeCipherMapping;
    
    /**
     * Constructor
     */
    public PropertyCipher(ITypeCipherAdapter defaultTypeCipher, Map specificTypeCipherMapping) {
        super();
        this.defaultTypeCipher = defaultTypeCipher;
        this.specificTypeCipherMapping = specificTypeCipherMapping;
    }
    
    /**
     * @param typeName
     * @return ITypeCipher
     * @throws PropertyCipherException
     */
    private ITypeCipherAdapter findTypeCipherFor(String typeName) {
        ITypeCipherAdapter retValue = (ITypeCipherAdapter) this.specificTypeCipherMapping.get(typeName);
        if (retValue == null) return this.defaultTypeCipher;
        return retValue;
    }

    /* (non-Javadoc)
     * @see ar.com.rab.beancipher.spi.IPropertyCipher#cipherProperty(java.lang.Object, ar.com.rab.beancipher.spi.IPropertyDescriptor)
     */
    public void cipherProperty(Object aBean, IPropertyDescriptor descriptor) throws PropertyCipherException {
        try {
            cipherOrDecipherProperty(aBean, descriptor, IConstants.DO_CIPHER);
        } catch (Exception e) {
            throw new PropertyCipherException(e);
        }
    }

    /* (non-Javadoc)
     * @see ar.com.rab.beancipher.spi.IPropertyCipher#deCipherProperty(java.lang.Object, ar.com.rab.beancipher.spi.IPropertyDescriptor)
     */
    public void deCipherProperty(Object aBean, IPropertyDescriptor descriptor) throws PropertyCipherException {
        try {
            cipherOrDecipherProperty(aBean, descriptor, IConstants.DO_DECIPHER);
        } catch (Exception e) {
            throw new PropertyCipherException(e);
        }
    }
    
    /**
     * @param aBean
     * @param descriptor
     * @param action
     * @throws PropertyCipherException
     * @throws IllegalArgumentException
     * @throws IllegalAccessException
     * @throws InvocationTargetException
     * @throws TypeCipherException
     */
    public void cipherOrDecipherProperty(Object aBean, IPropertyDescriptor descriptor, int action) throws PropertyCipherException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, TypeCipherException {
        ITypeCipherAdapter typeCipher = findTypeCipherFor(descriptor.getClassName());
        Object object = descriptor.getGetter().invoke(aBean, new Object[] {});
        if (object == null) return; 
        Object result = null;
        switch (action) {
            case IConstants.DO_CIPHER:
                result = typeCipher.cipher(object);
                break;
            case IConstants.DO_DECIPHER:
                result = typeCipher.deCipher(object);
                break;
        }
        descriptor.getSetter().invoke(aBean, new Object[] {result});
    }

}
